package com.apes.fn.scm.income.distributionOrder.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = DistributionOrder.CONST_TABLE_NAME)
public class DistributionOrder extends Model {
    public static final String CONST_TABLE_NAME = "distribution_order";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "订单分销", padding = 12)
    @Comment("订单分销")
    private String id;

    @Column(name = "retail_order_id", length = 64)
    @Comment("零售订单号")
    private String retailOrderId;

    @Column(name = "source_business_key", length = 64)
    @Comment("源单据号")
    private String sourceBusinessKey;

    @Column(name = "third_party_key", length = 64)
    @Comment("第三方订单号")
    private String thirdPartyKey;

    @Column(name = "receivable_date")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Comment("收款时间")
    private Date receivableDate;

    @Column(name = "sale_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售金额")
    private double saleAmount = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "year_card_dept_id", referencedColumnName = "id")
    @Comment("年卡使用门店")
    private Dept yearCardDept;

    @States(name = "源单据类型", values = {
            @State(id = "yearCard", name = "年卡"),
            @State(id = "service", name = "服务"),
            @State(id = "yearCardReturn", name = "零售退货（服务）"),
            @State(id = "serviceReturn", name = "零售退货（年卡）")})
    @Column(name = "state", length = 25, nullable = false)
    private String state; //状态

    @Column(name = "app_source_business_key", length = 64)
    @Comment("来源单据号")
    private String appSourceBusinessKey;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("所属公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "belong_dept_id", referencedColumnName = "id")
    @Comment("所属门店")
    private Dept belongDept;

    @Column(name = "distribution_commission_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("分销佣金")
    private double distributionCommissionAmount = 0;

    @Column(name = "distribution_service_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("分销服务费")
    private double distributionServiceAmount = 0;

    @Column(name = "manage_relief_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("分销管理费减免")
    private double manageReliefAmount = 0;

    @Column(name = "bear_platform", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("承担方(平台)")
    private double bearPlatform;

    @Column(name = "bear_dept", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("承担方(门店)")
    private double bearDept;

    @Column(name = "bear_distribution", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("承担方(分销员)")
    private double bearDistribution;

    @Column(name = "distribution_name", length = 128)
    @Comment("分销员")
    private String distributionName;

    @Column(name = "distribution_phone_number", length = 32)
    @Comment("电话号码")
    private String distributionPhoneNumber;

    @Column(name = "car_number", length = 32)
    @Comment("车牌号")
    private String carNumber;

    public void setDistributionCommissionAmount(double distributionCommissionAmount){
        this.distributionCommissionAmount = Arith.round(distributionCommissionAmount,2);
    }

    public void setDistributionServiceAmount(double distributionServiceAmount){
        this.distributionServiceAmount = Arith.round(distributionServiceAmount,2);
    }

    public void setManageReliefAmount(double manageReliefAmount){
        this.manageReliefAmount = Arith.round(manageReliefAmount,2);
    }

    public void setBearPlatform(double bearPlatform){
        this.bearPlatform = Arith.round(bearPlatform,2);
    }

    public void setBearDept(double bearDept){
        this.bearDept = Arith.round(bearDept,2);
    }

    public void setBearDistribution(double bearDistribution){
        this.bearDistribution = Arith.round(bearDistribution,2);
    }
}
